package com.ibm.spatiotemp.server.datasources.mockdb;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;

import org.apache.log4j.Logger;

import com.ibm.spatiotemp.server.connections.ConnectionManager;
import com.ibm.spatiotemp.server.datamodel.gpsdata.TSPoint;
import com.ibm.spatiotemp.server.datamodel.gpsdata.TSPointResultSet;
import com.ibm.spatiotemp.server.datamodel.gpsdata.mocking.MockingTSPointResutSet;
import com.ibm.spatiotemp.server.datasources.DataSchema;
import com.ibm.spatiotemp.server.datasources.DataSource;
import com.ibm.spatiotemp.server.datasources.RawDataAccessor;
import com.ibm.spatiotemp.server.framework.request.Request;
import com.ibm.spatiotemp.tools.DateParser;
import com.ibm.spatiotemp.tools.jsonParser;

public class MockdbRawDataAccessor extends RawDataAccessor {
	public static Logger logger = Logger.getLogger(MockdbRawDataAccessor.class);

	protected Connection conn;
	private MockdbQueryStringFormatter formatter_ptr=null;
	private Statement createtable_statement;
	private Statement datainsert_statement;
	private Statement clip_query_statement;
	public MockdbRawDataAccessor(
			DataSource src) {
		super(src);
		queryformatter = new MockdbQueryStringFormatter();
		formatter_ptr = (MockdbQueryStringFormatter)queryformatter;
	}

	@Override
	public void connect() throws Exception {
		conn = ConnectionManager.GetConnection(DataSource.getConnection_string());
		createtable_statement = conn.createStatement();
		datainsert_statement = conn.createStatement();
		clip_query_statement = conn.createStatement();
		
	}

	@Override
	public void close() throws Exception {
		createtable_statement.close();
		datainsert_statement.close();
		clip_query_statement.close();
//		conn.close();
	}

	public void createMovingObjectTable(){
		String sql = formatter_ptr.getCreateMovingObjectTableSQL(DataSource.getDataschema());
		logger.info(sql);
		try {
			createtable_statement.executeUpdate(sql);
		} catch (SQLException e) {
			logger.error(e.getMessage() + "\n detail: "
					+ jsonParser.toJsonString(e.getStackTrace()));
		}
		
	}
	
	@Override
	public TSPointResultSet ScanGPSData(String query_string) throws Exception {
		String sql = query_string;
		logger.info("ScanGPSData " + sql);
		ResultSet resset = null;
		TSPointResultSet tspointset = new MockingTSPointResutSet(resset);
		try {
			resset = clip_query_statement.executeQuery(sql);
			tspointset = new MockingTSPointResutSet(resset);
		} catch (SQLException e) {
			logger.error(e.getMessage() + "\n detail: "
					+ jsonParser.toJsonString(e.getStackTrace()));
		}
		return tspointset;
		}

	@Override
	public int WriteGPSData(String query_string, Collection<TSPoint> tsp_data) throws Exception {
		String sql = formatter_ptr.writeDataString(
				DataSource.getDataschema(),tsp_data);
		logger.info(sql);
		try {
			datainsert_statement.executeUpdate(sql);
		} catch (SQLException e) {
			logger.error(e.getMessage() + "\n detail: "
					+ jsonParser.toJsonString(e.getStackTrace()));
			return 0;/*data is not inserted*/
		}
		return tsp_data.size();
	}

	@Override
	public TSPointResultSet ScanGPSData(Request request) throws Exception {
		Statement st = clip_query_statement;
		ResultSet resultSet = null;
		String sqlStr = null;
		TSPointResultSet tsResultSet = null;
		DataSchema schema = DataSource.getDataschema();
		String modid = request.getId();
		String startts = request.getStartts();
		String endts = request.getEndts();
		startts = "" + DateParser.getTimestamp(startts);
		endts = "" + DateParser.getTimestamp(endts);
		
		sqlStr = queryformatter.RawDataScanQueryString(schema, startts, endts, modid);
		logger.info("hsql::ScanGPSData: " + sqlStr);
		
		resultSet = st.executeQuery(sqlStr);
		tsResultSet = new MockingTSPointResutSet(resultSet);
		return tsResultSet;
	}

}
